/**
 * 
 */
package com.alstudio.ui.module.voip;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.core.voip.CallDetail;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.config.BaseTitleActivityConfig;
import com.alstudio.view.dialog.AlertDialogCreator;
import com.alstudio.view.listener.AlDialogOnClickListener;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.loovee.imaohu.R;

/**
 * 与某人的通话详情
 * 
 * @author alonso lee
 * 
 */
public class CallDetailActivity extends TitleBarActivity implements
		RankViewLoadStateListener {

	private User user;

	// 显示用户数据的view
	private PullRefreshAndLoadMoreView listview;
	// 搜索结果
	private ArrayList<CallDetail> detailList;

	private CallDetailAdapter adapter;

	private boolean isForBaomihua = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();
		setupView(getApplicationContext(), getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarActivityContentView(R.layout.call_detail_activity);
		BaseTitleActivityConfig config = new BaseTitleActivityConfig();
		config.rightUseImageButton(true);
		setConfig(config);

		String msg = getString(R.string.TxtCallDetailTitle);
		if(user != null){
			msg = String.format(msg, user.getNick());
		}else{
			msg = getString(R.string.TxtBaomihuaRecord);
		}
		setCenterTextViewMessage(msg);
		listview = (PullRefreshAndLoadMoreView) findViewById(R.id.listview);
		// 禁止下拉刷新
		listview.disablePullRefresh();
		// 不要声音
		listview.setSoundEffectsEnabled(false);
		listview.disableItemClick();
		detailList = new ArrayList<CallDetail>();
		adapter = new CallDetailAdapter(detailList);
		listview.setAdapter(adapter);
		listview.setLoadStateListener(this);

		// rightUseImageButton(false);
		// hideRightButton(true);
		// setRightButtonName(R.string.TxtClear);

		setRightButtonImageSrc(R.drawable.me_title_icon_delete);
		hideRightButton(true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		user = (User) getIntent().getSerializableExtra("user");
		if (user == null) {
			// finish();
			isForBaomihua = true;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (!isUIinited) {
			isUIinited = true;
			requeqtData();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#requeqtData()
	 */
	@Override
	public void requeqtData() {
		// TODO Auto-generated method stub
		super.requeqtData();
		if (ALLocalEnv.isSendXmppDataEnable()) {
			showLoadingProgress();

			if (user != null) {
				ALUiXmppRequestCreator.postALUiXmppVoipCheckHistoryDetail(
						user.getJid(), ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "");
			} else {
				// 请求爆米花
				ALUiXmppRequestCreator.postALUiXmppCheckVoipRecord(
						ALUiXmppRequestCreator.CHECK_VOIP_RECORD_BAOMIHUA,
						ALLocalEnv.LOAD_MORE_START_INDEX + "",
						ALLocalEnv.LOAD_MORE_END_INDEX + "");
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleCheckCallHistoryDetailEvent(com
	 * .alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void handleCheckCallHistoryDetailEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleCheckCallHistoryDetailEvent(data);
		dismissLoadingProgress();
		listview.onLoadCompleted();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			isDataReceived = true;

			boolean hasMore = data.getBoolean();
			listview.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<CallDetail> tmp = (ArrayList<CallDetail>) data.getData();
			if (tmp != null && tmp.size() > 0) {
				detailList.addAll(tmp);
				adapter.notifyDataSetChanged();
				tmp.clear();
				tmp = null;
				hideRightButton(false);
			} else {
				// 没数据
				listview.showNoDataInfo(R.drawable.me_myphone_icon_empty02,
						getString(R.string.TxtNoCallDetailHint));

			}

		}
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.BaseActivity#handleGetBaoMiHuaVoipRecord(com.alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void handleGetBaoMiHuaVoipRecord(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleGetBaoMiHuaVoipRecord(data);
		dismissLoadingProgress();
		listview.onLoadCompleted();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			isDataReceived = true;

			boolean hasMore = data.getBoolean();
			listview.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<CallDetail> tmp = (ArrayList<CallDetail>) data.getData();
			if (tmp != null && tmp.size() > 0) {
				detailList.addAll(tmp);
				adapter.notifyDataSetChanged();
				tmp.clear();
				tmp = null;
				hideRightButton(false);
			} else {
				// 没数据
				listview.showNoDataInfo(R.drawable.me_myphone_icon_empty02,
						getString(R.string.TxtNoCallDetailHint));
			}
		}
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleLoginedEvent(com.alstudio.pojo
	 * .ALXmppEvent)
	 */
	@Override
	public void handleLoginedEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleLoginedEvent(data);
		if (!isDataReceived) {
			requeqtData();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		showClearCallDetailReqDialog();
	}

	@Override
	public void onLoadMore(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppVoipCheckHistoryDetail(
					user.getJid(), view.startIndex + "", view.endIndex + "");
		} else {
			listview.onLoadMoreCompleted();
		}
	}

	@Override
	public void onRefresh(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppVoipCheckHistoryDetail(
					user.getJid(), view.startIndex + "", view.endIndex + "");
		} else {
			listview.onRefreshCompleted();
		}
	}

	/**
	 * 显示清除通话记录确定对话框
	 */
	private void showClearCallDetailReqDialog() {
		String message = getString(R.string.TxtClearCallDetailAlert);
		message = String.format(message, user.getNick());
		final AlertDialogCreator mDialogCreator = AlertDialogCreator
				.createTwoButtonNormalDialog(this, false);
		mDialogCreator.setLeftKeyListener(new AlDialogOnClickListener() {

			@Override
			public void onClick() {
				clearCallDetail();
				finish();

			}
		});

		mDialogCreator.setMessage(message);

		mDialogCreator.showDialog();

	}

	/**
	 * 清除通话记录
	 */
	private void clearCallDetail() {
		// if (ALLocalEnv.isSendXmppDataEnable())
		{
			ALUiXmppRequestCreator.postALUiXmppVoipDeleteCallHistoryDetail(user
					.getJid());
		}
	}

}
